package middleware

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"runtime/debug"
	"shopstar/global"
	"shopstar/reponse"
)

func Recover() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		defer func() {
			if err := recover(); err != nil {
				global.Logs.Error(
					ctx.Request.URL.Path,
					zap.Any("error ", err),
					zap.String("stack", string(debug.Stack())),
				)
				reponse.FailWithMsg(string(debug.Stack()), ctx)
				return
			}
		}()
		ctx.Next()
	}
}
